package com.privatetime.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.privatetime.dao.BaseDao;
import com.privatetime.service.BaseService;

public abstract class BaseServiceImpl<E, PK> implements BaseService<E, PK> {

	protected final Logger logger = Logger.getLogger(getClass());

	private BaseDao<E, PK> baseDao;

	public void setBaseDao(BaseDao<E, PK> baseDao) {
		this.baseDao = baseDao;
	}

	public PK createEntity(E entity) throws Exception {
		try {
			PK id = (PK) baseDao.createEntity(entity);
			return id;
		} catch (Exception e) {
			throw new Exception("Exception for operation of create entity ");
		}
	}

	public void deleteEntity(E entity) throws Exception {
		try {
			baseDao.deleteEntity(entity);		
		} catch (Exception e) {
			throw new Exception("Exception for operation of delete entity ");
		}
	}

	public void deleteEntityById(PK id) throws Exception {
		deleteEntity(retrieveEntity(id));
	}

	public E retrieveEntity(PK id) {
		return baseDao.retrieveEntity(id);
	}

	public void updateEntity(E entity) throws Exception {
		try {
			baseDao.updateEntity(entity);
		} catch (Exception e) {
			throw new Exception("Exception for operation of update entity");
		}
	}

	public List<E> findEntityAll() {
		return baseDao.findEntityAll();
	}

}
